package com.gitee.neuray.security.admin.staff.biz;

import com.gitee.neuray.security.admin.dto.AdminOrgDto;
import com.gitee.neuray.security.admin.dto.AdminRoleDto;
import com.gitee.neuray.security.admin.dto.StaffDto;
import com.gitee.neuray.security.admin.org.entity.AdminOrgEntity;
import com.gitee.neuray.security.admin.org.mapper.AdminOrgMapper;
import com.gitee.neuray.security.admin.role.entity.AdminRoleEntity;
import com.gitee.neuray.security.admin.staff.vo.StaffOrg;
import com.gitee.neuray.security.admin.util.wechat.entity.AccessToken;
import com.gitee.neuray.security.admin.util.wechat.entity.UserInfo;
import com.gitee.neuray.security.admin.util.wechat.entity.UserInfoDetail;
import com.gitee.neuray.security.common.biz.BaseBiz;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import com.gitee.neuray.security.common.util.UUIDUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.gitee.neuray.security.admin.staff.mapper.StaffMapper;
import com.gitee.neuray.security.admin.staff.vo.StaffVo;


@Service
@Transactional(rollbackFor = Exception.class)
public class StaffBiz extends BaseBiz<StaffMapper, StaffVo> {
    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private AdminOrgMapper orgMapper;

    @Autowired
    private WechatBiz wechatBiz;

    /**
    * 分页获取信息
    * @param query
    * @return
    */
    public TableResultResponse page(Query query){
        Page<Object> result = PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StaffVo> list = staffMapper.findBypage(query);
        return new TableResultResponse(result.getPageSize(),result.getPageNum(),result.getPages(),result.getTotal(),list);
    }
    public List<StaffVo> nopage(Query query){
        return staffMapper.findBypage(query);
    }

    /**
     * 根据组织id获取组织下人员
     * @param id
     * @return
     */
    public List<StaffDto> findStaffByOrgId(String id){
        List<StaffDto> list = staffMapper.findStaffByOrgId(id);
        return list;
    }

    /**
     * 根据组织id获取组织下人员
     * @param
     * @return
     */
    public List<StaffVo> findStaffByOrg(StaffOrg staffOrg){
        List<StaffVo> list = staffMapper.findStaffByOrg(staffOrg);
        return list;
    }

    /**
     * 根据组织id获取组织下人员
     * @param staffOrg
     * @return
     */
    public List<StaffOrg> findOrgByOrg(StaffOrg staffOrg){
        List<StaffOrg> list = staffMapper.findOrgByOrg(staffOrg);
        return list;
    }

    /**
     * 暂时先按二级结构树查询组织结构
     * @return
     */
    public List<StaffOrg> getStaffByTree(){
        List<AdminOrgEntity> orgs = orgMapper.selectAll();
        List<StaffOrg> staffOrgs = new ArrayList<>();
        List<StaffOrg> staffOrgsC = new ArrayList<>();
        for(AdminOrgEntity org:orgs){
            if(org.getParentId().equals("root")){
                StaffOrg staffOrgR = new StaffOrg();
                staffOrgR.setId(org.getId());
                staffOrgR.setName(org.getOrgName());
                staffOrgs.add(staffOrgR);
            }
        }
        for(AdminOrgEntity org:orgs){
            if(!org.getParentId().equals("root")){
                StaffOrg staffOrgC = new StaffOrg();
                staffOrgC.setName(org.getOrgName());
                staffOrgC.setId(org.getId());
                staffOrgsC.add(staffOrgC);
            }
        }

        for (StaffOrg staffOrg:staffOrgs){
            staffOrg.setChildren(staffOrgsC);
        }

        return staffOrgs;
    }

    public void updateStaff(StaffVo entity) {
        super.updateById(entity);
    }

    public void insertStaff(StaffVo entity){

        String id = UUIDUtils.generateShortUuid();
        entity.setId(id);
        super.insert(entity);

    }

    public void deleteStaff(StaffVo entity){
        super.deleteById(entity.getId());

    }

    /**
     * 根据部门名获取部门信息
     * @param ORG_NAME
     * @return
     */
    public AdminOrgDto findOrgByOrgName(String ORG_NAME){
        return staffMapper.findOrgByOrgName(ORG_NAME);
    }

    /**
     * 根据人员名获取人员信息
     * @param NAME
     * @return
     */
    public StaffDto findStaffByName(String NAME){
        return staffMapper.findStaffByName(NAME);
    }

    /**
     * 根据人员id获取人员角色信息
     * @param id
     * @return
     */
    public List<Map> findRoleByUserId(String id){
        return staffMapper.findRoleByUserId(id);
    }


    /**
     * 根据人员id获取人员信息
     * @param id
     * @return
     */
    public StaffDto findStaffById(String id){
        return staffMapper.findStaffById(id);
    }

    /**
     * 根据部门id获取部门信息
     * @param id
     * @return
     */
    public AdminOrgDto findOrgById(String id){
        return staffMapper.findOrgById(id);
    }

    /**
     * 判断人员是否为公司成员
     * @param map
     * @return
     */
    public Map<String,Object> staffCheck(Map map){

        //微信企业号code标识
        String code = map.get("code").toString();
        //应用入口自定义参数
        String state = map.get("state").toString();

        //微信企业号AccessToken
        AccessToken accessToken = wechatBiz.getAccessToken(state);
        String token = accessToken.getAccessToken();

        //微信企业号人员信息
        UserInfo userInfo = wechatBiz.getUserInfo(code,token);

        //微信用户id
        String userId = userInfo.getUserId();

        if (userId == null || "".equals(userId)){
            map.put("msg","error");
            return map;
        }

        //微信企业号人员详情
        UserInfoDetail userInfoDetail = wechatBiz.getUserInfoDetail(userId,token);

        Map<String,String> ret = new HashMap<>();

        //微信userId即为电话号，通过其获取数据库中staff相关成员信息
        String phone = userInfoDetail.getMobile();
        StaffVo staffParam = new StaffVo();
        staffParam.setPhone(phone);
        //如果存在两个人公用同一手机号，只取一条。
        List<StaffVo> staffVos = staffMapper.findStaffByPhone(staffParam);

        StaffVo staffVo = staffVos.get(0);

        Map<String,Object> maprole = new HashMap();
        maprole.put("userId",staffVo.getId());

        Map<String,Object> staffInfo = new HashMap<>();
        staffInfo.put("phone",phone);
        staffInfo.put("name",staffVo.getName());
        staffInfo.put("deptCode",staffVo.getDeptCode());
        staffInfo.put("accout",staffVo.getAccout());
        staffInfo.put("email",staffVo.getEmail());
        staffInfo.put("sfzdz",staffVo.getSfzdz());
        staffInfo.put("zzzt",staffVo.getZzzt());
        staffInfo.put("id",staffVo.getId());
        staffInfo.put("orgName",staffVo.getOrgName());
        staffInfo.put("avatar",userInfoDetail.getAvatar());
        staffInfo.put("position",staffVo.getPosition());

        map.put("staffInfo", staffInfo);
        map.put("wxUserId",userId);
        map.put("wxPhone",phone);
        map.put("wxAccessToken",accessToken);
        map.put("msg","success");
        return map;

    }

    @Override
    protected String getPageName() {
        return null;
    }


}
